package com.qdxwx.web.controller.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.website.WebsiteProfileService;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.enums.CourseType;
import com.qdxwx.models.enums.WebsiteProfileType;
import com.qdxwx.models.member.MemberType;
import com.qdxwx.models.category.Category;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Course接口
 * @author developer
 */
@Controller
@RequestMapping("/course")
public class CourseListController extends BaseController {

	@Autowired
	private CourseService courseService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private WebsiteProfileService websiteProfileService;
	@Autowired
	private MemberTypeService memberTypeService;
	@Autowired
	private ChannelService channelService;

	@RequestMapping("/list.html")
	public String showCourseList(HttpServletRequest request, ModelMap model) {
		int channelId = TKConvert.toInteger(request.getParameter("channelId"),2);
		Channel channel = channelService.get(channelId);
		if (channel == null) {
			return toErrorView(request, "找不到对应的频道！");
		}
		return showCourseList(request, channel, model);
	}

	@RequestMapping("/{channelDir:[a-zA-Z]+}/list.html")
	public String showCourseList(HttpServletRequest request, @PathVariable("channelDir") String channelDir, ModelMap model) {
		Channel channel = channelService.getByDir(channelDir);
		if (channel == null) {
			return toErrorView(request, "找不到对应的频道！");
		}
		return showCourseList(request, channel, model);
	}

	private String showCourseList(HttpServletRequest request, Channel channel, ModelMap model) {
		//禁用的频道
		if (channel.getIsDisabled() == 1) {
			return toErrorView(request, "当前频道已被禁用！");
		}
		//判断是否属于模块
		if (channel.getModuleId() != ModuleType.COURSE.ordinal()) {
			return toErrorView(request, "当前频道归属错误！");
		}
		model.put("channel", channel);
		if (TKRequest.isMobileBrowse(request)){
			return getViewPathH5("course/course-list");
		}
		int channelId = channel.getChannelId();
		int categoryId = TKConvert.toInteger(request.getParameter("categoryId"));
		int categoryRootId = 0;
		int categoryParentId = 0;
		String pageTitle = channel.getChannelName();
		if (categoryId > 0) {
			Category category = categoryService.get(categoryId);
			if (category == null) {
				categoryId = 0;
			} else {
				channelId = category.getChannelId();
				if (category.getParentId() == 0) {
					categoryRootId = categoryId;
				} else {
					categoryRootId = category.getParentId();
				}
				categoryParentId = category.getParentId();
				pageTitle = category.getCategoryName() + "-" + pageTitle;
				model.put("category", category);
			}
		}
		model.put("categoryRootId", categoryRootId);//根目录id
		model.put("channel", channel);
		// 查询所有1级专业
		List<Category> categoryRoots = categoryService.getRoots(channelId);
		model.put("categoryRoots", categoryRoots);
		//查询子专业
		if (categoryId > 0) {
			List<Category> categoryChildren;
			//父Id大于0,获得它的同级专业
			if (categoryParentId > 0) {
				categoryChildren = categoryService.getChildren(categoryRootId);
			} else {
				//获得它的子级专业
				categoryChildren = categoryService.getChildren(categoryId);
			}
			model.put("categoryChildren", categoryChildren);
		}
		model.put("pageTitle", pageTitle);
		model.put("categoryRootId", categoryRootId);//一级Id
		model.put("categoryParentId", categoryParentId);//父级id
		model.put("categoryId", categoryId);//id

		CourseQuery params = new CourseQuery();
		params.setChannelId(channelId);
		params.setCategoryId(categoryId);
		params.setIsLiving(0);
		params.setTeacherId(TKConvert.toInteger(request.getParameter("teacherId")));
		params.setMemberTypeId(TKConvert.toInteger(request.getParameter("memberTypeId")));
		params.setSellType(CourseType.getString(request.getParameter("sellType")));
		params.setIsFree(TKConvert.toInteger(request.getParameter("isFree")));
		//只查询上架的
		params.setIsAvailable(1);
		if ("true".equals(request.getParameter("sellTypeExclude"))) {
			params.setSellTypeExclude("LIVE");//如果是课程+套餐，直接排除直播
		}
		String searchKey = TKString.trim(request.getParameter("searchKey"));
		if (searchKey.length() > 1) {
			params.setCourseName(TKString.searchKeyFilter(searchKey));
		}
		params.setOrderBy(TKConvert.toInteger(request.getParameter("orderBy")));

		// 页面传来的数据放到page中
		PageRequest page = new PageRequest();
		page.setCurrentPage(TKConvert.toInteger(request.getParameter("page")));
		page.setPageSize(12);
		int total = courseService.count(params);

		model.put("totalPages", page.getTotalPages(total));

		// 搜索课程列表
		List<Course> courseList = courseService.getList(params, page);
		model.put("courseList", courseList);

		Map<String, Object> serviceSwitch = websiteProfileService.getMap(WebsiteProfileType.serviceSwitch.toString());
		if ("ON".equals(serviceSwitch.get("member")) && ObjectUtils.isNotNull(memberTypeService)) {
			/*会员类型集合*/
			List<MemberType> memberTypeList = memberTypeService.getList(new MemberType(), null);
			model.put("memberTypeList", memberTypeList);
		}
		model.put("page", page);
		model.put("params", params);
		if (channel.getChannelId() == 2) {
			return getViewPath("course/course-list");
		} else {
			return getViewPath(String.format("course/course-%s-list", channel.getChannelDir()));
		}
	}
}